package com.microcdyUser.service.impl;
import com.microcdyUser.dao.SysUserOnlineMapper;
import com.microcdy.sys.pojo.SysUserOnline;
import com.microcdyUser.service.SysUserOnlineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:cdy
 * @Description:SysUserOnline业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SysUserOnlineServiceImpl implements SysUserOnlineService {

    @Autowired
    private SysUserOnlineMapper sysUserOnlineMapper;


    /**
     * SysUserOnline条件+分页查询
     * @param sysUserOnline 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SysUserOnline> findPage(SysUserOnline sysUserOnline, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sysUserOnline);
        //执行搜索
        return new PageInfo<SysUserOnline>(sysUserOnlineMapper.selectByExample(example));
    }

    /**
     * SysUserOnline分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SysUserOnline> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SysUserOnline>(sysUserOnlineMapper.selectAll());
    }

    /**
     * SysUserOnline条件查询
     * @param sysUserOnline
     * @return
     */
    @Override
    public List<SysUserOnline> findList(SysUserOnline sysUserOnline){
        //构建查询条件
        Example example = createExample(sysUserOnline);
        //根据构建的条件查询数据
        return sysUserOnlineMapper.selectByExample(example);
    }


    /**
     * SysUserOnline构建查询对象
     * @param sysUserOnline
     * @return
     */
    public Example createExample(SysUserOnline sysUserOnline){
        Example example=new Example(SysUserOnline.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysUserOnline!=null){
            // 用户会话id
            if(!StringUtils.isEmpty(sysUserOnline.getSessionId())){
                    criteria.andEqualTo("sessionId",sysUserOnline.getSessionId());
            }
            // 登录账号
            if(!StringUtils.isEmpty(sysUserOnline.getLoginName())){
                    criteria.andEqualTo("loginName",sysUserOnline.getLoginName());
            }
            // 部门名称
            if(!StringUtils.isEmpty(sysUserOnline.getDeptName())){
                    criteria.andEqualTo("deptName",sysUserOnline.getDeptName());
            }
            // 登录IP地址
            if(!StringUtils.isEmpty(sysUserOnline.getIpaddr())){
                    criteria.andEqualTo("ipaddr",sysUserOnline.getIpaddr());
            }
            // 登录地点
            if(!StringUtils.isEmpty(sysUserOnline.getLoginLocation())){
                    criteria.andEqualTo("loginLocation",sysUserOnline.getLoginLocation());
            }
            // 浏览器类型
            if(!StringUtils.isEmpty(sysUserOnline.getBrowser())){
                    criteria.andEqualTo("browser",sysUserOnline.getBrowser());
            }
            // 操作系统
            if(!StringUtils.isEmpty(sysUserOnline.getOs())){
                    criteria.andEqualTo("os",sysUserOnline.getOs());
            }
            // 在线状态on_line在线off_line离线
            if(!StringUtils.isEmpty(sysUserOnline.getStatus())){
                    criteria.andEqualTo("status",sysUserOnline.getStatus());
            }
            // session创建时间
            if(!StringUtils.isEmpty(sysUserOnline.getStartTimestamp())){
                    criteria.andEqualTo("startTimestamp",sysUserOnline.getStartTimestamp());
            }
            // session最后访问时间
            if(!StringUtils.isEmpty(sysUserOnline.getLastAccessTime())){
                    criteria.andEqualTo("lastAccessTime",sysUserOnline.getLastAccessTime());
            }
            // 超时时间，单位为分钟
            if(!StringUtils.isEmpty(sysUserOnline.getExpireTime())){
                    criteria.andEqualTo("expireTime",sysUserOnline.getExpireTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        sysUserOnlineMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SysUserOnline
     * @param sysUserOnline
     */
    @Override
    public void update(SysUserOnline sysUserOnline){
        sysUserOnlineMapper.updateByPrimaryKey(sysUserOnline);
    }

    /**
     * 增加SysUserOnline
     * @param sysUserOnline
     */
    @Override
    public void add(SysUserOnline sysUserOnline){
        sysUserOnlineMapper.insert(sysUserOnline);
    }

    /**
     * 根据ID查询SysUserOnline
     * @param id
     * @return
     */
    @Override
    public SysUserOnline findById(String id){
        return  sysUserOnlineMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SysUserOnline全部数据
     * @return
     */
    @Override
    public List<SysUserOnline> findAll() {
        return sysUserOnlineMapper.selectAll();
    }
}
